layui.use(['admin', 'ax', 'table', 'form'], function () {
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;

    var basePath = system.contextPath + "/develop/commonSdk";

    var Current = {
        tableId: "table"
    };

    Current.initColumn = function () {
        return [[
            {type: 'numbers'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'name', sort: true, title: '名称'},
            {field: 'title', sort: true, title: '标题'},
            {field: 'contactName', sort: true, title: '联系名称'},
            {field: 'contactWay', sort: true, title: '联系方式'},
            {field: 'createdTime', sort: true, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作', width: 280}
        ]];
    };

    Current.search = function () {
        var queryData = {};
        table.reload(Current.tableId, {where: queryData});
    };

    Current.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(basePath + "/delete/id/" + data.id, function (res) {
                if (res.status) {
                    system.success("删除成功!");
                    table.reload(Current.tableId);
                } else {
                    system.error("删除失败!" + res.msg + "!");
                }
            }, function (res) {
                system.error("删除失败!" + res.msg + "!");
            });
            ajax.start();
        };
        system.confirm("是否删除?", operation);
    };

    Current.onEdit = function (data) {
        var url = basePath + '/edit?id=' + data.id;

        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑',
            area: ['60%', '70%'],
            content: system.contextPath + url,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };


    Current.onAdd = function (data) {
        var url = basePath + '/toAdd';

        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加',
            area: ['60%', '70%'],
            content: url,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Current.tableId,
        url: basePath + '/search/page',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Current.initColumn(),
        parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data.records //解析数据列表
            };
        }
        , size: 'lg',
        done: function (res, curr, count) {
            count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
            layui.each($('select'), function (index, item) {
                var elem = $(item);
                elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
            });
            form.render();
        },
        response: {
            statusCode: 200
        }
    });

    // 添加按钮
    $('#btnAdd').click(function () {
        Current.onAdd();
    });

    // 监听修改update到表格中
    form.on('select(statusFilter)', function (data) {
        var data = buildUpdateData($(data.elem).attr("data-id"), "status", data.value);
        updateBatchById(data);
    });


    window.changeSortValue = function (e) {
        $e = $(e);
        var data = buildUpdateData($e.attr("data-id"), $e.attr("name"), $e.val());
        updateBatchById(data);
    };

    function buildUpdateData(currentId, filed, filedValue) {
        var data = {'id': currentId};
        data[filed] = filedValue;
        return data;
    }

    function updateBatchById(data) {
        var ajax = new $ax(basePath + "/edit/id/" + data.id, function (res) {
            if (res.code == 200) {
                system.success("修改成功！");
                //重新加载表格
                table.reload(Current.tableId);

            } else {
                system.error("失败：" + res.msg);
            }
        }, function (res) {
            system.error("失败：" + res.msg);
        });
        ajax.set(data);
        ajax.start();
    }


    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            Current.onDelete(data);
        } else if (layEvent === 'edit') {
            Current.onEdit(data);
        } else if (layEvent === 'info') {
            Current.onH5Current(data);
        }
    });
});